import { ts } from 'qvog-dsl';
import {
  native,
  HomeCheckHint,
} from 'qvog-dsl';

const SEVERITY = 2;
const DOC_PATH = 'docs/unsafe-random.md';
const DESCRIPTION = 'Usage of insecure random number generator Math.random() is prohibited. Please use a secure random number generator instead.';

export default native<HomeCheckHint>()()
  .match((node): node is ts.CallExpression => {
    return ts.isCallExpression(node);
  })
  .when((node: ts.CallExpression) => {
    if (!ts.isPropertyAccessExpression(node.expression)) { return false };

    const expr = node.expression;
    const isMathRandom = expr.expression.getText() === 'Math' && expr.name.getText() === 'random';

    return isMathRandom;
  })
  .report({
    severity: SEVERITY,
    description: DESCRIPTION,
    docPath: DOC_PATH,
  });
